<?php
/**
 * 财务查看会员
 */
namespace Finance\Controller;
use Think\Controller;
class IndexController extends Controller {

	// 会员列表
	public function Index(){
		// 获取等级列表
		$level_list = getMemberLevelList(true);

		$key = I('k');
		if( empty($key) || $key !== 'wgw' ){
			exit();
		}

		$member_level = I('member_level',-1,'intval');
		$start_time = strtotime(I('start_time'));
		$end_time = strtotime(I('end_time'));
		$where = array();

		if (!empty($member_name)) {
			$where['username'] = array('LIKE', '%'.$member_name.'%');
		}

		if ($level_list[$member_level]) {
			$where['level'] = $member_level;
		}


		if (!empty($start_time) && !empty($end_time)) {
			// 如果有开始及结束时间 则取中值
			$where['createtime'] = array(array('EGT', $start_time), array('ELT', $end_time));
		}else{
			if (!empty($start_time)) {
				// 有开始时间 
				$where['createtime'] = array('EGT', $start_time);
			}elseif (!empty($end_time)) {
				// 有结束时间 
				$where['createtime'] = array('ELT', $end_time);
			}
		}

		$total = M('user')->where($where)->count();
		
		$Page = $this->setPageHander($total);
		$show = $Page?$Page->show():'';
		
		$member_list = M('user')->field('id,mobile,createtime,level,username,weixin,status,super_agent')
						->where($where)
						->limit($Page->firstRow.','.$Page->listRows)
						->order('id desc')
						->select();
		// foreach ($member_list as $key => $value) {
		// 	$member_list[$key]['mobile'] = substr($value['mobile'],0,3).'*****'.substr($value['mobile'], 8);
		// }
		$this->assign(array(
			'level_list' => $level_list,	// 等级列表
			'member_list' => $member_list,
			'member_name' => $member_name,
			'member_level' => $member_level,
			'start_time' => $start_time,
			'end_time' => $end_time,
			'pager'=>$show,
			'total'=>$total,
		));
		
		$this->display();
	}

	// 分页配置返回分页类
	protected function setPageHander($count,$getRows=15){
		if (!$count || !is_numeric($count)) {
			return false;
		}
		$Page  = new \Think\Page($count,$getRows);
		// 分页配置设置
		$Page->setConfig('prev','上一页');
		$Page->setConfig('next','下一页');
		$Page->setConfig('first','首页');
		$Page->setConfig('last','末页');
		
		return $Page;
	}
}